<?php

if (!defined('IN_XLP')) {
    exit('Access Denied!');
}

/**
 * Description of forumAction
 *
 * @author xlp
 */
class forumAction extends commonAction {

    private $cacheFilePre = 'forum/visit_vid_'; //保存缓存信息的文件名前缀
    private $cacheTagsFile = 'forum/tags_'; //保存标签缓存
    private $info = array('否', '<span class="red">是</span>');
    private $tagsStatus = array('小区标签', '商圈标签');
    private $status = array('<span class="red">屏蔽</span>', '正常');

    function __construct() {
        parent::__construct();
        parent::_checkLogin();
    }

    function index() {
        $title = $this->_get('q');
        $stime = $this->_get('stime');
        $etime = $this->_get('etime');
        $p = $this->_getid('p', 1);
        $uid = $this->_getid('uid', 0);
        $village_id = $this->_getid('village_id', 0);
        $hot = $this->_getid('hot', 0);
        $top = $this->_getid('top', 0);
        $report = $this->_getid('report', 0);
        $status = $this->_getid('status', 0);
        $recycle = false; //是否回收站
        $where = array();
        if ($stime && $etime) {
            $stime = inTime($stime);
            $etime = inTime($etime) + (24 * 60 * 60) - 1;
        }
        if ($stime && $etime) {
            $where['infotime[>=]'] = $stime;
            $where['infotime[<=]'] = $etime;
        }
        if ($title) {
            $where['LIKE'] = array('title' => parent::safeSearch($title));
        }
        if ($uid) {
            $where = array('uid' => $uid);
        }
        if ($village_id) {
            $where = array('village_id' => $village_id);
        }
        if ($hot) {
            $where = array('is_hot' => $hot);
        }
        if ($top) {
            $where = array('is_top' => $top);
        }
        if ($report) {
            $where = array('is_report' => $report);
        }
        if ($status) {
            $where = array('status' => 0);
            $recycle = true;
        } else {
            $where['status'] = 1;
        }
        $rs = M('forum_thread')->where($where)->page($p)->order('fid DESC')->findAll();
        $total = M('forum_thread')->getTotal();
//        z(M('area')->getAllSql());
        if ($rs) {
            foreach ($rs as $k => $v) {
                $rs[$k]['user_name'] = getUser($v['uid']);
                $rs[$k]['village_name'] = parent::getVillageName($v['village_id']);
            }
        }
        $this->assign(array('rs' => $rs, 'total' => $total, 'p' => $p, 'info' => $this->info, 'status' => $this->status, 'recycle' => $recycle));
//        print_r($this->getAllVar());
        $this->display();
    }

    function edit() {
        $fid = $this->_getid('fid');
        if (!$fid) {
            $this->returnJson = false;
            showError('参数丢失');
        }
        $rs = M('forum_thread')->where(array('fid' => $fid))->find();
        if (!$rs) {
            $this->returnJson = false;
            showError('主题不存在或者已被删除');
        }
        $this->assign(array('rs' => $rs));
        $this->display();
    }

    function save() {
        $fid = $this->_postid('fid');
        $title = $this->_post('title');
        $content = $this->_post('content');
        if (!$fid) {
            $this->returnJson = false;
            showError('参数丢失');
        }
        if (!$title) {
            $this->returnJson = false;
            showError('帖子标题不能为空');
        }
        if (!$content) {
            $this->returnJson = false;
            showError('帖子内容不能为空');
        }
        $rs = M('forum_thread')->where(array('fid' => $fid))->find();
        if (!$rs) {
            $this->returnJson = false;
            showError('主题不存在或者已被删除');
        }
        M('forum_thread')->update(array('title' => $title, 'content' => $content), array('fid' => $fid));
        $this->JsonReturn('ok', null, 1);
    }

    function setting() {
        $id = $this->_postid('id');
        $isHot = $this->_postid('is_hot', 0);
        $isTop = $this->_postid('is_top', 0);
        $isOk = $this->_postid('is_ok', 0);
        $where = array();
        if (!$id) {
            showError('参数丢失');
        }
        if ($isHot) {
            $where = $isHot == 1 ? array('is_hot' => 1) : array('is_hot' => 0);
        }
        if ($isTop) {
            $where = $isTop == 1 ? array('is_top' => 1) : array('is_top' => 0);
        }
        if ($isOk) {
            $where = array('status' => 1);
        }
        if ($where) {
            M('forum_thread')->update($where, array('fid' => $id));
            $this->JsonReturn('ok', null, 1);
        } else {
            $this->JsonReturn('error');
        }
    }

    function detail() {
        $fid = $this->_getid('fid');
        if (!$fid) {
            showError('参数丢失');
        }
        $rs = M('forum_thread')->where(array('fid' => $fid))->find();
        if (!$rs) {
            showError('主题不存在或者已被删除');
        }
        $rs['content'] = express($rs['content']);
        $rs['lbs_info'] = json_decode($rs['lbs_info'], true);
        $rs['pics_list'] = $rs['photo_counts'] ? D('album')->getPhotosById($rs['uid'], $rs['pics_list']) : array();
//        z($rs);
        $this->assign(array('rs' => $rs));
        $this->display();
    }

    function delete() {
        $id = $this->_postid('id');
        if (!$id) {
            showError('参数丢失');
        }
        //检查主题是否存在
        $forum = M('forum_thread')->field('uid,title,village_id')->where(array('fid' => $id))->find();
        if (!$forum) {
            $this->JsonReturn('主题不存在，请检查');
        }
        M('forum_thread')->update(array('status' => 0), array('fid' => $id));
//        F($this->cacheFilePre . $forum['village_id'], null); //清除统计缓存
        $this->JsonReturn('ok', null, 1);
    }

    function clear() {
        $id = $this->_postid('id');
        if (!$id) {
            showError('参数丢失');
        }
        //检查主题是否存在
        $forum = M('forum_thread')->field('uid,title,village_id')->where(array('fid' => $id))->find();
        if (!$forum) {
            $this->JsonReturn('主题不存在，请检查');
        }
        M('forum_thread')->delete(array('fid' => $id));
//        //删除所有回帖
        M('forum_post')->delete(array('fid' => $id));
//        //删除点赞
        M('forum_praise')->delete(array('fid' => $id));
//        F($this->cacheFilePre . $forum['village_id'], null); //清除统计缓存
        $this->JsonReturn('ok', null, 1);
    }

    /*
     * 标签管理列表
     */

    function tags_list() {
        $p = $this->_getid('p', 1);
        $type = $this->_getid('type', 0);
        $where = array('city_id' => admin::$adminInfo['city_id']);
        if ($type) {
            $where['stype'] = ($type - 1);
        }
        $rs = M('forum_tags')->where($where)->page($p)->order('sort DESC')->findAll();
        $total = M('forum_tags')->getTotal();
        $this->assign(array('rs' => $rs, 'total' => $total, 'p' => $p, 'type' => $type, 'status' => $this->tagsStatus));
        $this->display();
    }

    /*
     * 添加标签
     */

    function tags_add() {
        $type = $this->_getid('type', 0);
        $this->assign(array('type' => $type, 'status' => $this->tagsStatus));
        $this->display();
    }

    /*
     * 录入标签
     */

    function tags_post() {
        $objData = array();
        $objData['aid'] = admin::$adminInfo['aid'];
        $objData['city_id'] = admin::$adminInfo['city_id'];
        $objData['stype'] = $this->_postid('stype', 0);
        $objData['tag_title'] = $this->_post('tag_title');
        $objData['sort'] = $this->_postid('sort', 0);
        if (!$objData['tag_title']) {
            $this->JsonReturn('标签不能为空');
        }
        //判断重复
        if (M('forum_tags')->where(array('tag_title' => $objData['tag_title'], 'city_id' => $objData['city_id'], 'stype' => $objData['stype']))->find()) {
            $this->JsonReturn('标签已存在，请检查');
        }
        M('forum_tags')->insert($objData);
        $this->tags_cache();
        $this->JsonReturn('ok', null, 1);
    }

    /*
     * 编辑标签
     */

    function tags_edit() {
        $id = $this->_getid('id', 0);
        if (!$id) {
            showError('参数丢失');
        }
        $rs = M('forum_tags')->where(array('city_id' => admin::$adminInfo['city_id'], 'tag_id' => $id))->find();
        if (!$rs) {
            showError('标签不存在');
        }
        $this->assign(array('rs' => $rs, 'status' => $this->tagsStatus));
        $this->display();
    }

    /*
     * 保存标签
     */

    function tags_save() {
        $id = $this->_postid('id', 0);
        if (!$id) {
            showError('参数丢失');
        }
        $rs = M('forum_tags')->where(array('city_id' => admin::$adminInfo['city_id'], 'tag_id' => $id))->find();
        if (!$rs) {
            showError('区域不存在');
        }
        $objData = array();
        $objData['stype'] = $this->_postid('stype', 0);
        $objData['tag_title'] = $this->_post('tag_title');
        $objData['sort'] = $this->_postid('sort', 0);
        if (!$objData['tag_title']) {
            $this->JsonReturn('标签不能为空');
        }
        //判断重复
        $hasTags = M('forum_tags')->where(array('tag_title' => $objData['tag_title'], 'city_id' => admin::$adminInfo['city_id'], 'stype' => $objData['stype']))->getField('tag_id');
        if ($hasTags && $hasTags != $id) {
            $this->JsonReturn('标签已存在，请检查');
        }
//        z($objData);
        M('forum_tags')->update($objData, array('city_id' => admin::$adminInfo['city_id'], 'tag_id' => $id));
        $this->tags_cache();
        $this->JsonReturn('ok', null, 1);
    }

    /*
     * 删除标签
     */

    function tags_delete() {
        $id = $this->_postid('id');
        if (!$id) {
            showError('参数丢失');
        }
        $rs = M('forum_tags')->where(array('city_id' => admin::$adminInfo['city_id'], 'tag_id' => $id))->find();
        if (!$rs) {
            showError('标签不存在');
        }
        //检查标签是否有人使用，有使用则不能删除
        $hasTags = M('forum_thread')->field('fid')->where(array('tag_id' => $id))->find();
        if ($hasTags) {
            showError('该标签已被使用，不能删除');
        }
        M('forum_tags')->delete(array('city_id' => admin::$adminInfo['city_id'], 'tag_id' => $id));
        $this->tags_cache();
        $this->JsonReturn('ok', null, 1);
    }

    /*
     * 生成标签缓存
     */

    private function tags_cache() {
        $rs = M('forum_tags')->where(array('city_id' => admin::$adminInfo['city_id']))->order('sort DESC')->findAll();
        if (!$rs) {
            return false;
        }
        $objData = array();
        foreach ($rs as $val) {
            $objData[$val['stype']][$val['tag_id']] = $val['tag_title'];
        }
        F($this->cacheTagsFile . admin::$adminInfo['city_id'], $objData);
        return true;
    }

}
